
<template>
  <el-card style="height: 86vh">
    <el-row>
      <el-col>
        <el-date-picker
          v-model="gkDate"
          type="date"
          @change="handleTimePickChange"
          value-format="YYYY-MM-DD"
          format="YYYY年MM月DD日"
          placeholder="选择日期"
        >
        </el-date-picker>
        <el-select
        style="width: 10%;margin-left: 20px;"
        v-model="region"
        placeholder="请选择时段"
        clearable
      >
          <el-option label="0-8" value="0-8" />
          <el-option label="8-14" value="8-14" />
          <el-option label="14-19" value="14-19" />
          <el-option label="19-24" value="19-24" />
      </el-select>
      <el-button style="margin-left:20px" type="primary" @click="onSubmit">查 询</el-button>
      </el-col>
     
    </el-row>

    <el-input
      type="textarea"
      style="margin-top: 20px"
      :rows="16"
      v-model="gkDesc"
      v-loading="dataLoading"
    ></el-input>
    <el-button type="primary" style="margin-top: 20px" @click="copy"
      >复制</el-button
    >
  </el-card>
</template>
<script setup>
import { onMounted, ref } from "vue";
import utils from "@/utils/utils";
import store from "@/store";
import {
  createDailyData,
  getAll,
  getPeriodicData,
  getPeriodic,
} from "@/api/work-order";
import { isSameDate } from "@/utils/common";

let headers = {};
const upLoadLoading = ref(false);
const UploadExcelDialogShow = ref(false);
const total = ref(0);
const page = ref(1);
const size = ref(10);
const tableData = ref([]);
const dataLoading = ref(false);
const selectTimeDialogShow = ref(false);
const timeModelValue = ref("");
const gkDate = ref("");
const gkDesc = ref("");
const region=ref('');

const truncateString = (str, maxLength = 10) => {
  if (str.length > maxLength) {
    return str.substring(0, maxLength) + "...";
  }
  return str;
};

const getData = async () => {
  dataLoading.value = true;
  const result = await getPeriodic({
    time: gkDate.value,
    startH:region.value.split('-')[0],
    endH:region.value.split('-')[1]
  });
  // gkDesc.value = generateDailyReport(result, gkDate.value);
  gkDesc.value = result
  dataLoading.value = false;
};
// const getTime = async () => {
//   gkDate.value = (await getPeriodic()).substring(0, 10);
// };
// getTime().then(() => {
//   getData();
// });

const handleProgress = (file) => {
  upLoadLoading.value = true;
};

const beforeUploadHandle = (file) => {
  utils.showLoadding("正在上传...");
  return true;
};

const handleSuccess = async (res) => {
  if (res.content === 0) {
    utils.showSuccess("上传数据成功");
  }

  UploadExcelDialogShow.value = false;
  utils.hideLoadding();
};
const handleTimePickChange = (value) => {
  gkDate.value = value;
  // getData();
};
const onSubmit= ()=>{
  getData();
}
const copy = () => {
  navigator.clipboard.writeText(gkDesc.value);
  utils.showSuccess("复制成功");
};

function generateDailyReport(data, dateStr) {
  const date = new Date(dateStr);
  const dateText = `时段报数【${region.value.split('-')[0]}:00-${region.value.split('-')[1]}:00】`;

  // 工具函数
  const groupBy = (arr, key) =>
    arr.reduce((acc, item) => {
      (acc[item[key]] = acc[item[key]] || []).push(item);
      return acc;
    }, {});
const updownsummarize=(arr)=>{
  const regex = /^-?\d+(\.\d+)?%$/;
  console.log(regex.test(arr),"1111111");
  if(regex.test(arr)){
    const decimal = Number(arr.replace("%", "")) / 100;
  // return decimal>1 ?`上升${arr}`:decimal <1 ?`下降${arr}`:`持平`
  return decimal>=1 ?`上升${arr}`:`下降${arr}`
  }else{
   return arr>=1 ?`上升${arr}`: arr < 0 ? `下降${arr}`:`持平`
  }
}
;
  const summarize = (arr) =>
    arr
      .map(
        (item) => `${item.cityBureau}${item.num > 1 ? item.num + "宗" : "1宗"}`
      )
      .join("、");

  const summarizeBusiness = (arr) =>
    arr.map((item) => `${item.businessName}${item.num}宗（${item.proportion}）`).join("，");

  const totalRequestText= `【整体诉求】本时段共${data.totalRequest[0].thisPeriodNums}宗，累计${data.totalRequest[0].total}宗，同比前日${updownsummarize(data.totalRequest[0].contrast)}。`
  const transferRequestText=`【转办诉求】本时段共${data.transferRequest[0].thisPeriodNums}宗，累计${data.transferRequest[0].total}宗，同比前日${updownsummarize(data.transferRequest[0].contrast)}。`
  const provinceControlText =`【省级管控】应审${data.provinceControl[0].review}宗，超时审核${data.provinceControl[0].timeoutReview}宗，审核及时率${data.provinceControl[0].passRate}。`
  const provinceDisposalText =`【省级处置审核】应审${data.provinceDisposal[0].review}宗，超时审核${data.provinceDisposal[0].timeoutReview}宗，审核及时率${data.provinceDisposal[0].passRate}。`
  const followUpTrackingText =`【后续跟踪审核】应审${data.followUpTracking[0].review}宗，超时审核${data.followUpTracking[0].timeoutReview}宗，审核及时率${data.followUpTracking[0].passRate}。`
  const followUpFeedbackText =`【后续跟踪反馈审核】应审${data.followUpFeedback[0].review}宗，超时审核${data.followUpFeedback[0].timeoutReview}宗，审核及时率${data.followUpFeedback[0].passRate}。`
  const hotBusinessText=`【热点业务】客户来电主要集中在${summarizeBusiness(data.hotBusiness[0].periodicReportMessage)}`
  // const periodicReportCityText=`主要集中在${summarize(data.hotBusiness[0].periodicReportCity)}`
  return `${dateText}\n${totalRequestText}\n${transferRequestText}\n${provinceControlText}\n${provinceDisposalText}\n${followUpTrackingText}\n${followUpFeedbackText}\n${hotBusinessText}。`;
}

onMounted(() => {
  headers = { "X-Token": store.getters.token };
});
</script>
